package com.dingding.web.activity;

import javax.annotation.Resource;

import com.dingding.common.core.beans.ActivityRelevance;
import com.dingding.common.core.beans.UserLogin;
import com.dingding.facade.user.service.IUserInfoFacade;
import com.dingding.facade.user.service.IUserLoginFacade;
import com.dingding.facde.activity.service.IActivityShareFacade;
import com.dingding.facde.common.IPhonevalidateService;
import com.dingding.facde.coupon.ICouponService;
import com.dingding.web.base.action.BaseAction;

/**
 * @ClassName: ManageCoupon
 * @Description: 优惠券管理action
 * @author cw
 * @date 2016年2月4日 上午11:24:34
 * @version 叮叮搭车2.0
 */
@SuppressWarnings("serial")
public class ActivityShareAction extends BaseAction {

	// 声明活动分享业务逻辑的接口
	@Resource(name = "activityShareFacade")
	protected IActivityShareFacade userActivityFacade; // 活动分享接口

	// 声明活动分享业务逻辑的接口
	@Resource(name = "userInfoFacade")
	protected IUserInfoFacade userInfoFacade; // 活动分享接口

	// 声明活动分享业务逻辑的接口
	@Resource(name = "userLoginFacade")
	protected IUserLoginFacade userLoginFacade; // 活动分享接口

	// 声明活动分享业务逻辑的接口
	@Resource(name = "couponService")
	protected ICouponService couponService; // 优惠券接口
	
	//声明手机验证码接口
	@Resource(name = "phonevalidateService")
	protected IPhonevalidateService phonevalidateService; //手机验证码接口

	/**
	 * @Title: getUserActivity
	 * @Description: 获取活动信息
	 * @author cw
	 * @return String
	 */
	public String getUserActivity() {

		String userId = getPara("userId");
		String activityId = getPara("activityId");

		ActivityRelevance userActivity = userActivityFacade.getUserActiveInfo(userId, activityId);

		String result = null;

		if (userActivity != null && !userActivity.equals("")) {
			result = super.generateResponseData(200, "success", userActivity);
		} else {
			result = super.generateResponseData(201, "用户活动列表不存在", null);
		}

		super.write(result);

		return NONE;
	}

	/**
	 * 
	 * @Title: addUserActivity
	 * @Description: 修改用户统计次数
	 * @param userActivity
	 *            用户活动
	 * @author cw
	 * 
	 */
	public String setUserActivityNumber() {

		String userId = getPara("userId");
		String activityId = getPara("activityId");

		boolean flag = userActivityFacade.setUserActivityNumber(userId, activityId);

		String result = null;

		if (flag) {
			result = super.generateResponseData(200, "success", flag);
		} else {
			result = super.generateResponseData(201, "用户活动列表不存在", null);
		}

		super.write(result);

		return NONE;
	}

	/**
	 * 
	 * @Title: addUserActivity
	 * @Description: 添加用户活动信息
	 * @param userActivity
	 *            用户活动s
	 * @author cw
	 * @return 返回用户活动信息邀请码
	 */
	public String addUserActivityCode() {

		String telphone = getPara("tellphone");
		String userId = getPara("userId");
		String activityId = getPara("activityId");

		// 判断用户活动关系是否存在
		ActivityRelevance activityRelevance = userActivityFacade.getUserActiveInfo(userId, activityId);

		String invitationCode = "";

		// 存在 ， 直接返回邀请码
		if (activityRelevance == null) {
			// 不存在， 生成 关联关系，并返回邀请码
			boolean flag = userActivityFacade.addUserActivity(userId, activityId);

			if (!flag) {
				return "";
			}
		}

		// 存在 ， 直接返回邀请码
		invitationCode = activityRelevance.getInvitationCode();

		return invitationCode;

	}

	/**
	 * @Title: getShareCode
	 * @Description: 获取用户活动编码
	 * @return String
	 * @throws @author
	 *             cw
	 */
	public String getShareCode() {

		String userId = getPara("userId");
		String activityId = getPara("activityId");

		String invitationCode = "";

		// 获取用户活动关系是否存在
		ActivityRelevance activityRelevance = userActivityFacade.getUserActiveInfo(userId, activityId);

		if (activityRelevance != null) {
			invitationCode = activityRelevance.getInvitationCode();
		} else {
			// 用户邀请码生成
			boolean flag =  userActivityFacade.addUserActivity(userId, activityId);
			
			if (flag) {
				activityRelevance = userActivityFacade.getUserActiveInfo(userId, activityId);
				invitationCode = activityRelevance.getInvitationCode();
			}
		}

		super.write(invitationCode);

		return NONE;
	}

	/**
	 * 
	 * @Title: addUserCoupon
	 * @Description: 用户推荐注册送优惠券
	 * @param userActivity
	 *            用户活动
	 * @author cw
	 * 
	 */
	public String addUserInfoCoupon() {

		String telphone = getPara("tellphone");
		String userId = getPara("userId");
		String activityId = getPara("activityId");
		String verifyCode = getPara("verifyCode");

		//判断验证码是否正确
		boolean isSuccess = this.phonevalidateService.verifyCode(telphone, verifyCode);
		if (!isSuccess) {
			super.write("invalidVerifyCode");
			return NONE;
		}
		
		//判断用户是否注册过
		boolean isUserReg = this.userInfoFacade.userIsRegedByPhone(telphone);

		if(isUserReg){
			super.write("userAlreadyReg");
			return NONE;
		}
			
		
		// 送用户优惠券
		// 1 . 注册用户 和被注册用户都有一张 10元优惠券， 2 推荐用户也只能有一张优惠券
		boolean flag = userInfoFacade.addUser(telphone, "");
		if (flag) {

			// 推荐用户 ： 是否已经参加过活动， 第一次参加送券，以后就不送了
			// 1. 判断用户是否已经领过券， 领过券，不管，没有领过券，送一张券
			ActivityRelevance activityRelevance = userActivityFacade.getUserActiveInfo(userId, activityId);

			// 老用户的处理逻辑
			if (activityRelevance != null) {

				if (activityId.equals("98") == true) { // 统计欢乐谷门票
					// 老用户增加统计次数
					userActivityFacade.setUserActivityNumber(userId, activityId);

				} else if (activityId.equals("99") == true) { // 送券活动
					if (activityRelevance.getIsAttendActivity() == 1) { // 未参加活动
						// 老用户送券。
						couponService.addUserCoupon(userId);
						// 更改 活动关系表，用户是否已经参加过活动
						userActivityFacade.setActivityRelevanceState(userId, activityId);
					}
				}
			}

			// 新用户注册成功送券
			UserLogin userLogin = userLoginFacade.getUserLoginByTelPhone(telphone);
			if (userLogin != null) {
				// 注册用户 ： 每次都送券。
				couponService.addUserCoupon(userLogin.getUserName());// 给用户添加优惠券
				// 新用户生成注册码，并修改领券的状态
				userActivityFacade.setActivityRelevanceState(userLogin.getUserName(), activityId);
			}
		}

		return NONE;
	}

	/**
	 * 
	 * @Title: addUserInfo
	 * @Description: 用户推荐注册统计
	 * @param userActivity
	 *            用户活动
	 * @author cw
	 * 
	 */
	public String addUserInfoNumber(String telPhone, String userId, String activityId) {

		// 1. 新用户注册
		boolean flag = userInfoFacade.addUser(telPhone, "");

		// 2. 用户注册成功以后，增加 用户活动关联信息
		if (flag) {
			// 修改推荐用户统计数量
			flag = userActivityFacade.setUserActivityNumber(userId, activityId);

			// 新用户 根据用户 电话号码查询 userId , 送用户一张 10元券 
			couponService.addUserCoupon(telPhone);// 给用户添加优惠券
		}

		return NONE;
	}

}
